const {User} = require('../../../models/User');
const multiparty = require("multiparty");
const Path = require('path');

module.exports = async (req, res) => {
    try{
      var form = new multiparty.Form({uploadDir: Path.join(__dirname, '../', '../', "../",'public','uploads',"images","avatar"),
      keepExtensions:true});
      form.parse(req, async (err, fields, files)=>{
        var { size, path } = files.file[0]
        if(size>200000){
          res.send({errCode:"1",msg:"图片尺寸过大"})
        }else{
          var fileName = Path.basename(path)
          var filePath = "/uploads/images/avatar/" + fileName;
          const update = await User.updateOne({user_id:fields.user_id[0]},{$set:{"avatar": filePath}})
          res.send({errCode:"0",msg:"头像修改成功",data:{"avatar":filePath}})
        }
      });
      
    }catch(e){
      console.log(e)
        res.status(400).send({errCode:"1",msg:"修改失败"})
    }
}